﻿@model  RBAC.Web.Models.DomainModel
@using RBAC.Web.CustomHelpers
@using RBAC.Web.Mapper
@using RBAC.Web.Utilities
@{
    ViewBag.Title = "Edit Domain";

    string returnUrl = Url.Action("ListDomain", "Domain");
}
@section Styles {
    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/form.js?3")" type="text/javascript"></script>
    <link href="@Url.Content("~/Content/themes/base/jquery.ui.all.css")" rel="stylesheet" />
    <link href="@Url.Content("~/Content/themes/base/jquery-ui.css")" rel="stylesheet" type="text/css" />
    <link href="@Url.Content("~/Content/themes/base/jquery.ui.datepicker.css")" rel="stylesheet" type="text/css" />
    @*<link href="@Url.Content("~/Content/site2.css")" rel="stylesheet" type="text/css" />*@

    <script type="text/javascript">
        $(function () {
            //How to setup Datepicker
            SetDatePicker("FreeDate");
            
            //How to setup Lookup
			
            //Master Detail
			 
							 
							 
							 
				 
            SetOneToManyFor("Function", '@Url.Action("DeleteFunctions", "Function")', '@Url.Action("EditDomain", "Domain", new { DomainName = Model.DomainName })', "form-Domain");
        });
    </script>
}

<h2>Domain Detail</h2>
@using (Html.BeginForm("Update", "Domain", FormMethod.Post, new { @id = "form-Domain", enctype = "multipart/form-data" }))
{
    @Html.ValidationSummary(false)
    <fieldset>
        <legend>DomainModel</legend>


        <div class="two-column-container">
            @Html.HiddenFor(model => model.Id)


            <div class="texteditor">
                @Html.LabelFor(model => model.DomainName)
                @Html.TextBoxFor(model => model.DomainName)
                @Html.HiddenFor(model => model.Id)
                <input type="hidden" name="Mode" value="Edit" />
                <input type="hidden" name="OldValue" value="@(Model.DomainName == null ? "" : Model.DomainName)" />
            </div>


            <div class="texteditor">
                @Html.LabelFor(model => model.Description)
                @Html.TextBoxFor(model => model.Description)
            </div>



        </div>
        <div class="bottom-menu clear">
            <input type="button" name="btnBack" value="Cancel" id="button-back" onclick="location.href='@returnUrl    '"/>
            <input type="Button" value="Save" id="button-save" onclick="JavaScript: SaveButtonClick()" />
            <input type="button" name="btnDelete" value="Delete" id="button-delete" onclick="JavaScript: DeleteButtonClick()" />
        </div>
    </fieldset>

    // Master Detail Dest Class

    bool highlightFunction = false;
    if (ViewData["FunctionId"] != null)
    {
        try
        {
            int index = Model.Functions.IndexOf(Model.Functions.FirstOrDefault(m => m.Id == Convert.ToInt32(ViewData["FunctionId"])));
            if (index > -1)
            {
                var item = Model.Functions.ElementAt(index);
                Model.Functions.RemoveAt(index);
                Model.Functions.Insert(0, item);

                highlightFunction = true;
            }
        }
        catch { }
    }
    var Functiongrid = new WebGrid(source: Model.Functions, canPage: false);
    if (highlightFunction)
    {
        Functiongrid.SelectedIndex = 0;
    }
    <input type="hidden" id="Function_hid_delete" value="Delete" name="DeleteButton" /> 
    <fieldset>
        @Html.Hidden("Domain_Id", Model.Id)
        <legend>Function</legend>
        <div class="top-menu">
            <input id="Function-button-add" type="button" value="New" onclick="location.href='@Url.Action("Add", "Function", new { Domain_id = Model.Id, DomainName = Model.DomainName })    '"/>
            <input type="button" value="Delete" id="Function-button-delete" />
        </div>
        <div id="Function-confirm-delete" title="Confirm delete." style="display: none">
            <span>Do you want to delete?</span>
        </div>
        <div id="Function-dialog-alert" title="Delete Function" style="display: none">
            <span>No Function was selected.</span>
        </div>

        @Html.DisplayMessage(Convert.ToString(ViewData["FunctionMessage"]), ViewData["FunctionStatus"] as bool?)

        @if (Model.Functions != null && Model.Functions.Count > 0)
        {    
            <div style="overflow: scroll; width: auto; height: auto; max-height: 400px;">
                @Functiongrid.GetHtml(
        htmlAttributes: new { id = "Function" },
        tableStyle: "tableMain Functiongrid",
        rowStyle: "row-a",
        alternatingRowStyle: "row-b",
        selectedRowStyle: "selected-row",
        columns: Functiongrid.Columns(
            Functiongrid.Column(header: "", style: "width_5per", format: @<text><input name="FunctionCheckbox" type="checkbox" value="@item.Id" /></text>),
            Functiongrid.Column(header: "#", style: "width_0per", format: item => item.WebGrid.Rows.IndexOf(item) + 1)
        //TODO : สามารถ Edit ได้ --
        // ทดสอบ Edit  
        					
			
			
												
							
						@* ถ้า attribute ของลูก เป็น LinkEdit *@
            , Functiongrid.Column(header: "Function Name", style: "width_0per", columnName: "FuncName",
            format: item =>
            {
                string link = "<a href=\"{0}?FuncName={2}\" class=\"word_wrap\">{1}</a>";
                return Html.Raw(string.Format(link, @Url.Content("~/Function/EditFunction"), item.Value.FuncName, item.Value.FuncName));
            })
			
			
												
							
			
			
			           @* ถ้า attribute ของลูก ไม่เป็น LinkEdit , DataType เป็น String *@
, Functiongrid.Column(header: "Description", canSort: false, style: "width_100per", columnName: "Description",
                format: item =>
                {
                    string Description = "<div class=\"word_wrap\">{0}</div>";
                    return Html.Raw(string.Format(Description, String.IsNullOrWhiteSpace(item.Description) ? "" : item.Value.Description));
                })   
			

			
						
							
			
			
						@* START ดึง External Lookup Field *@
									@* ถ้า Lookup attribute เป็น String *@
, Functiongrid.Column(header: "Domain", canSort: false, style: "width_100per", columnName: "DomainDomainName",
                format: item =>
                {
                    string DomainName = "<div class=\"word_wrap\">{0}</div>";
                    return Html.Raw(string.Format(DomainName, String.IsNullOrWhiteSpace(item.DomainDomainName) ? "" : item.Value.DomainDomainName));
                })  

						@* END ดึง External Lookup Field *@
			 
			

						
							
			
			
												
							
			
			
												
							
			
			
						@* START ดึง External Lookup Field *@
									@* ถ้า Lookup attribute เป็น String *@
  
        //chayawooht.puy
             //, Functiongrid.Column(header: "DomainLookupDomainName",canSort: false, style: "width_100per", columnName: "DomainLookupDomainName",
             //    format: item =>
             //    {
             //        string DomainName = "<div class=\"word_wrap\">{0}</div>";
             //        return Html.Raw(string.Format(DomainName, String.IsNullOrWhiteSpace(item.DomainLookupDomainName) ? "" : item.Value.DomainLookupDomainName));
             //    })  
        
       
						@* END ดึง External Lookup Field *@
			 
			

						
		             
))
            </div>
        }
        else
        {
            <div class="emptyRow">No Function for this Domain.</div>
        }

    </fieldset>

}

<div id="confirm-delete" title="Confirm delete." style="display: none">
    <span>Do you want to delete?</span>
</div>

<div id="confirm-save" title="Confirm save." style="display: none">
    <span>Do you want to save?</span>
</div>

<script type="text/javascript">
    function DeleteButtonClick() {
        $("#confirm-delete").dialog({
            modal:true,
            buttons: {
                Yes: function () {
                    $(this).dialog("close");
                    $.post('@Url.Action("DeleteDomainById", "Domain")',$("#form-Domain").serialize(),function (data) {
                        var s = '@returnUrl'
                        if(s.indexOf("?") !== -1)
                            window.location.href = s + '&result=' + data + '&operation=delete';   
                        else
                            window.location.href = s + '?result=' + data + '&operation=delete'; 
                    });
                },
                No:function(){
                    $(this).dialog("close");
                }
            }
        })
    }

    function SaveButtonClick() {
        $("#confirm-save").dialog({
            modal: true,
            buttons: {
                Yes: function () {
                    $(this).dialog("close");
                    $("#form-Domain").submit();
                },
                No: function () {
                    $(this).dialog("close");
                }
            }
        })
    }
</script>
